package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.util.RedisUtil;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.sound.midi.Soundbank;
import java.util.*;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@Service
//@EnableAspectJAutoProxy
public class UserServiceImpl implements UserService{

    @Autowired
    private RedisUtil  redisUtil;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    /**
     * 实现用户的登录的操作
     * 1.明文转化为密文MD5加密
     * 2.通过username和password查询数据库
     * 3.
     *     有数据:用户名和密码正确,返回UUID token
     *     没有数据:用户名和密码错误  返回null
     * @param user
     * @return
     */
    @Override
    public String login(User user) {

        String password = user.getPassword();
        //md5hash相对与md5更加安全
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(s);
        QueryWrapper<User>  queryWrapper=new QueryWrapper(user);
        User userDB = userMapper.selectOne(queryWrapper);


        if(userDB==null){
            return null;
        }
        String token= UUID.randomUUID().toString().replaceAll("-", "");
        return token;
    }


    @Override
    public PageResult getUserPage(PageResult pageResult) {
        QueryWrapper<User>  queryWrapper=new QueryWrapper<User>();
        boolean b = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(b, "username", pageResult.getQuery());
        IPage<User>  page=new Page<User>();
        page.setSize(pageResult.getPageSize());
        page.setCurrent(pageResult.getPageNum());

        IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);
        pageResult.setRows(userIPage.getRecords());
        pageResult.setTotal(userIPage.getTotal());
         return pageResult;
    }

    @Override
    @Transactional //1.运行期异常  2,编译器异常
    public boolean addUser(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        int insert = userMapper.insert(user);

        return   (insert==1);
    }

    @Override
    public void deleteUser(Integer id) {
           userMapper.deleteById(id);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public PageResult getUserPageByRedis(PageResult pageResult) {
        long size = redisUtil.lGetListSize("user");
        //判断redis中是否有数据
        if(size==0) {
            List<User> all = this.findAll();
            for (Object u : all) {
                redisUtil.lSet("user",u );
            }
        }
        long rows = redisUtil.lGetListSize("user");
        //当前的页数
        Integer pageNum = pageResult.getPageNum();
        //每页的大小
        Integer pageSize = pageResult.getPageSize();
        //查询的记录为: (pageNum-1)*pageSize +1  =>  pageNum*pageSize
        List<Object> users = redisUtil.lGet("user", (pageNum-1)*pageSize +1, pageNum*pageSize);
        pageResult.setRows(users);
        pageResult.setTotal(size);
        return pageResult;
    }

    //获取用户的角色的信息
    @Override
    public  PageResult  getUserRolePage(PageResult pageResult){
        QueryWrapper<User>  queryWrapper=new QueryWrapper();
        String []  list={"id","username","role_id"};
        queryWrapper.select(list);
        Page<User>  page=new Page();
        page.setSize(pageResult.getPageSize());
        page.setCurrent(pageResult.getPageNum());
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);

        pageResult.setRows(userPage.getRecords());
        pageResult.setTotal(userPage.getTotal());
        return  pageResult;
    }



}
